#include <cstdint>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;
using ll = int64_t;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string S, T;
    cin >> S >> T;
    ll m;
    cin >> m;
    ll lenT = T.size();
    
    while (m--) {
        ll l, r;
        cin >> l >> r;
        ll lenA = r - l + 1;
        ll start = l - 1;
        vector<ll> dp(lenT + 1);
        for (ll j = 0; j <= lenT; j++) {
            dp[j] = j;
        }
        
        for (ll i = 0; i < lenA; i++) {
            vector<ll> ndp(lenT + 1);
            ndp[0] = i + 1;
            char c = S[start + i];
            for (ll j = 1; j <= lenT; j++) {
                ll cost = (c == T[j-1]) ? 0 : 1;
                ll op1 = dp[j] + 1;
                ll op2 = ndp[j-1] + 1;
                ll op3 = dp[j-1] + cost;
                ndp[j] = min({op1, op2, op3});
            }
            dp = std::move(ndp);
        }
        cout << dp[lenT] << '\n';
    }
    return 0;
}